# 激活函数
def Sigmoid(x):
    return 1. / (1. + np.exp(-x))
# 定义训练后的模型
def xor_model(xy):
    x1 = xy[:, :, 0:1]
    x2 = xy[:, :, 1:]
    linear0_0 = x1 * 6.1440 + x2 * 6.1724 + (-9.2932)
    linear0_1 = x1 * 8.9765 + x2 * 9.7823 + (-4.4537)
    hidden0_0 = Sigmoid(linear0_0)
    hidden0_1 = Sigmoid(linear0_1)
    out = hidden0_0 * (-7.8489) + hidden0_1 * (7.3517) + (-3.3070)
    pred = Sigmoid(out)
    return pred
# 绘制3D图像
figure = plt.figure(figsize=(8,8))
ax = figure.add_subplot(111, projection='3d')
# 设置X,Y坐标值
xy = np.linspace(-0.5, 1.5, 51)
x_grid, y_grid  = np.meshgrid(xy, xy) # (51,51)
xy_grid = np.stack([x_grid, y_grid], axis=-1) # (51,51,2)
# 使用训练好的模型,输出三维的Z坐标值
z_xor = xor_model(xy_grid) # z_xor:(51,51,1)
z_xor = z_xor.squeeze() # z_xor:(51,51)
# 绘制三维拟合图
ax.plot_surface(x_grid, y_grid, z_xor, edgecolor='none', cmap='rainbow', alpha=0.5)
# 绘制样本数据三维散点图
ax.scatter(x[0], y[0], z[0], c='r',marker='o', s=100) #形状圆形
ax.scatter(x[1], y[1], z[1], c='b',marker='s', s=100) #形状正方形
ax.scatter(x[2], y[2], z[2], c='b',marker='s', s=100) #形状正方形
ax.scatter(x[3], y[3], z[3], c='r',marker='o', s=100) #形状圆形
# 三维坐标X,Y,Z设置
ax.set_xticks([0, 1])
ax.set_xlabel('X')
ax.set_yticks([0, 1])
ax.set_ylabel('Y')
ax.set_zticks([0, 1])
ax.set_zlabel('Z')
plt.show()
